package feature;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ken
 * Date: 2023-11-26
 * Time: 19:17
 */
public class DelOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要删除的图书名字");
        String name = scanner.nextLine();
        int index = -1;
        int countsize = bookList.getUsedSize();
        for (int i = 0; i < countsize; i++) {
            Book book = bookList.getBooks(i);
            if(book.getName().equals(name)) {
             index = i;
                break;
            }
        }
        if (index == -1) {
            System.out.println("没有找到你输入的名字的图书");
        }
        for (int j = index; j < countsize-1; j++) {
            Book temp = bookList.getBooks(j+1);
            bookList.setBooks(j,temp);
        }
        bookList.setBooks(countsize-1,null);
        bookList.setUsedSize(countsize-1);
        System.out.println("删除成功");
    }
}
